package utils 
{
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class ArrayUtil 
	{
		
		public function ArrayUtil() 
		{
			
		}
		
		public static function cloneShapeArray(shapePoints:Array):Array {
			if (shapePoints == null) {
				return null;
			}
			
			var clonePoints:Array = [];
			var pointsXY:Array;
			var pointsXYClone:Array;
			for (var i:int = 0; i < shapePoints.length; i++)
			{
				pointsXY = shapePoints[i];
				pointsXYClone = [];
				for (var j:int = 0; j < pointsXY.length; j++)
				{
					pointsXYClone.push(pointsXY[j]);
					//log(i + ", " + j + " = " + pointsXY[j]);
				}
				clonePoints.push(pointsXYClone);
			}
			
			return clonePoints;
		}
		
		public static function getShapeRect(shapePoints:Array):Rectangle {
			
			var rect:Rectangle ;
			var pointsXY:Array;
			var maxX:Number = Number.MIN_VALUE;
			var maxY:Number = Number.MIN_VALUE;
			var minX:Number = Number.MAX_VALUE;
			var minY:Number = Number.MAX_VALUE;
			var j:int;
			var count:int;
			for (var i:int = 0; i < shapePoints.length; i++)
			{
				pointsXY = shapePoints[i];
				count = pointsXY.length;
				j = 0;
				while(j < count)
				{
					maxX = Math.max(maxX, pointsXY[j]);
					maxY = Math.max(maxY, pointsXY[j + 1]);
					minX = Math.min(minX,  pointsXY[j]);
					minY = Math.min(minY, pointsXY[j + 1]);
					j += 2;
				}
				
			}
			rect = new Rectangle(minX, minY, maxX - minX, maxY - minY);
			return rect;
		}
		
		public static function scaleArrayXY(arr:Array, factor:Number):void {
			if (arr == null ) {
				return;
			}
			for (var i:int = 0; i < arr.length; i++)
			{
				arr[i] *= factor;
			}
		}
		
		public static function scaleArrays(arr:Array, factor:Number):void {
			if (arr == null ) {
				return;
			}
			for (var i:int =0 ; i < arr.length; i++)
			{
				scaleArrayXY(arr[i], factor);
			}
		}
		
		public static function scaleShapePoints(shapePoints:Array, factorX:Number, factorY:Number):void {
			if (shapePoints == null ) {
				return;
			}
			
			var j:int;
			var count:int;
			var pointsXY:Array;
			for (var i:int = 0; i < shapePoints.length; i++)
			{
				pointsXY = shapePoints[i];
				count = pointsXY.length;
				j = 0;
				while(j < count)
				{
					pointsXY[j] *= factorX;
					pointsXY[j + 1] *= factorY;
					j += 2;
				}
				
			}
		}
		
		public static function shiftShapeArray(shapePoints:Array, shiftX:Number , shiftY:Number):void {
			if (shapePoints == null ) {
				return;
			}
			
			var j:int;
			var count:int;
			var pointsXY:Array;
			for (var i:int = 0; i < shapePoints.length; i++)
			{
				pointsXY = shapePoints[i];
				count = pointsXY.length;
				j = 0;
				while(j < count)
				{
					pointsXY[j] += shiftX;
					pointsXY[j + 1] += shiftY;
					j += 2;
				}
				
			}
		}
		
	}

}